package com.baidu.springboot_10_simpledemo.component;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//拦截器（登录检查）
public class LoginHandlerInterceptor implements HandlerInterceptor {
    //目标执行方法前
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //取出session中存入的loginUser对象
        Object loginUser = request.getSession().getAttribute("loginUser");
        //判断
        if(loginUser == null){
            //没有对象，未登录，返回登录界面
            request.setAttribute("msg","对不起，您尚未登录");
            request.getRequestDispatcher("/index.html").forward(request,response);
            return false;
        }else{
            //已登录，放行
            return true;
        }

    }
}
